package com.android.BluethootChess.util;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;

import com.android.BluethootChess.connectivity.IComunication;
import com.android.BluethootChess.model.PieceColor;
import com.android.BluethootChess.model.PieceTypeAndColor;

public class Utils {
	//private static final String CONNECTION_PROPERTIES = "Connection.properties";
	//private static final String IP = "ip";
	public static int BOARD_HEIGHT;
	public static int BOARD_WIDTH;
	public static int COLOR_TO_IGNORE;
	public static int HIGHLIGHT_COLOR;
	public static IComunication COMUNICATION=null;
	public static PieceColor PLAYING_COLOR;
	public static int PORT=1234;
	public static ServerSocket serverSock = null;
	
	public static final byte BOARD_SIZE = 8;

	/**
	 * Returneaza lista cu toate piesele albe.
	 * */
	public static List<Integer> getWhitePieces() {
		return getPiecesBetween(PieceTypeAndColor.values().length / 2,
				PieceTypeAndColor.values().length);
	}

	/**
	 * Returneaza lista cu toate piesele negre.
	 * */
	public static List<Integer> getBlackPieces() {
		return getPiecesBetween(0, PieceTypeAndColor.values().length / 2);
	}

	private static List<Integer> getPiecesBetween(int a, int b) {
		List<Integer> pieces = new ArrayList<Integer>();
		for (int i = a; i < b; i++) {
			pieces.add(i);
		}
		return pieces;
	}

	/**Gaseste culoarea pentru o piesa.
	 * @param pieceCode - codul piesei 
	 * */
	public static PieceColor getPieceColor(int pieceCode) {
		if (pieceCode > (PieceTypeAndColor.values().length-1) / 2)
			return PieceColor.WHITE;
		return PieceColor.BLACK;
	}
	
	public static void logFile(String s) {
		BufferedWriter bw = null;
		FileWriter fw = null;

		try {
			fw = new FileWriter("/sdcard/MyApp/log.txt", true);
			bw = new BufferedWriter(fw);
			bw.write(s);
			bw.newLine();

		} catch (IOException e) {
		} finally {
			try {
				bw.close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
	}
	
	public static void logFileError(Throwable e) {
		BufferedWriter bw = null;
		FileWriter fw = null;

		try {
			fw = new FileWriter("/sdcard/MyApp/log.txt", true);
			bw = new BufferedWriter(fw);
			bw.newLine();

			PrintWriter pw=new PrintWriter("/sdcard/MyApp/logError.txt");
			e.printStackTrace(pw);
			
		} catch (IOException t) {
		} finally {
			try {
				bw.close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
	}

	/**
	 * Returns the IP address of the server from a properties file.
	 * 
	 * @return The IP of the server
	 */
	public static String getServerIP(){
		/*Properties properties = new Properties();
		
		try {
			properties.load(new FileInputStream(CONNECTION_PROPERTIES));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return properties.getProperty(IP);*/
		return "172.30.112.83";
	}
	
	public static String getBluetoohServerAddress(){
		//return "01:1E:45:33:29:C6";
		String address=null;
		
		final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
		Set<BluetoothDevice> devices=adapter.getBondedDevices();
		
		for (BluetoothDevice bluetoothDevice:devices){
			address=bluetoothDevice.getAddress();
		}
		
		return address;
	}
}
